<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>正在为您跳转到支付页面，请稍候...</title>
	<style type="text/css">
body{margin:0;padding:0}
p{position:absolute;left:50%;top:50%;height:35px;margin:-35px 0 0 -160px;padding:20px;font:bold 16px/30px "宋体",Arial;background:#f9fafc url(/assets/img/loading.gif) no-repeat 20px 20px;text-indent:40px;border:1px solid #c5d0dc}
#waiting{font-family:Arial}
	</style>
</head>
<?php
$is_defend=true;
$nosession = true;
require './includes/common.php';
$submit2=true;

@header('Content-Type: text/html; charset=UTF-8');

$typeid=intval($_GET['typeid']);			// 标识是支付宝还是微信, 1
$trade_no=daddslashes($_GET['trade_no']);
$order=$DB->getRow("SELECT * FROM pre_order WHERE trade_no='{$trade_no}' LIMIT 1");
if(!$order)sysmsg('该订单号不存在，请返回来源地重新发起请求！');

$userrow = $DB->getRow("SELECT gid,mode FROM pre_user WHERE uid='{$order['uid']}' LIMIT 1");	// 1, 0

// sysmsg('userrow is ' . json_encode($userrow));				// userrow is false; but why ???
// sysmsg('order["uid"] is ' . json_encode($order['uid']));		// order["uid"] is "1000", why ???

// 获取订单支付方式ID、支付插件、支付通道、支付费率
/* 这里返回 ['typeid'=>$typeid, 'typename'=>$typename, 'plugin'=>$plugin, 
			'channel'=>$channel, 'rate'=>$money_rate, 'apptype'=>$apptype] */
$submitData = \lib\Channel::submit2($typeid, $userrow['gid']);

if($submitData){
	if($userrow['mode']==1 && $order['tid']!=4 || $order['tid']==2){				// order['tid'] 在 ajax.php 中强行为 3.
		$realmoney = round($order['money']*(100+100-$submitData['rate'])/100,2);
		$getmoney = $order['money'];
	}else{
		// sysmsg('走这里....');
		$realmoney = $order['money'];
		$getmoney = round($order['money']*$submitData['rate']/100,2);
	}
	// 以下一行, 大括号的写法是php 这边处理的, 跟 mysql 没有关系.
	// 字符串中的 大括号(curly brackets) 用来获取变量的值 {$...}  or ${...}, 类似于 javascript 中的 ``.
	// Simply write the expression the same way as it would appear outside the string, and then wrap it in { and }.
	// A string is a sequence of bytes or characters, enclosed within either single quote ( ' ) or double quote ( " ) characters.
	$DB->exec("UPDATE pre_order SET type='{$submitData['typeid']}',channel='{$submitData['channel']}',realmoney='$realmoney',getmoney='$getmoney' WHERE trade_no='$trade_no'");
}else{
	sysmsg('<center>当前支付方式无法使用</center>', '跳转提示');
}

$order['realmoney'] = $realmoney;							// 
$order['type'] = $submitData['typeid'];						// 1
$order['channel'] = $submitData['channel'];					// -1
$order['typename'] = $submitData['typename'];				// alipay 
$order['apptype'] = explode(',',$submitData['apptype']);	// null 

$loadfile = \lib\Plugin::load2($submitData['plugin'], 'submit', $trade_no);		// 正常进入了 alipay 的插件 的 submit 文件
$channel = \lib\Channel::get($order['channel']);
if(!$channel || $channel['plugin']!=PAY_PLUGIN)sysmsg('当前支付通道信息不存在');
$channel['apptype'] = explode(',',$channel['apptype']);
$ordername = !empty($conf['ordername'])?ordername_replace($conf['ordername'],$order['name'],$order['uid']):$order['name'];
// sysmsg(json_encode($ordername));	// 支付测试
include $loadfile;		// 这里就开始调用插件了吗?, 确实是这里..., The inlcude statement includes and evaluates the specified script
?>
<p>正在为您跳转到支付页面，请稍候...</p>
</body>
</html>